import Vue from 'vue'
import MToast from './index.vue'
let instance = null
function sleep(delay = 50) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve()
    }, delay)
  })
}
export default {
  show(text, delay = 1500) {
    if (instance) return
    const MToastConstructor = Vue.extend(MToast)
    instance = new MToastConstructor({
      el: document.createElement('div'),
      propsData: {
        text
      }
    })

    document.body.appendChild(instance.$el)

    setTimeout(async () => {
      instance.$el.style.opacity = '0'
      instance.$destroy()
      await sleep(1000)
      instance.$el.remove()
      instance = null
    }, delay)
  }
}
